#ifndef RPC_CLIENT_H_
#define RPC_CLIENT_H_

#include "base/base.h"
#include "include/rpc_def.h"

namespace urpc {
class HandlerMap;
class Connector;
class EventManager;
class ClientProcessor;

class RpcClient : public google::protobuf::RpcChannel {
  public:
    RpcClient(EventManager* poller, const std::string& server_ip, uint16 server_port,
              const std::string& client_ip, uint16 client_port, uint32 timeout);
    ~RpcClient();

    bool Init();
    void StopStreamFromClient();

    void SetReconnectClosure(Closure* c) {
      reconnect_cb_.reset(c);
    }

    void CallMethod(const MethodDescriptor* method, RpcController* controller,
                    const Message* request, Message* response,
                    google::protobuf::Closure* done);

    bool SwitchServer(const std::string& ip, uint16 port);

    bool Reconnect();

  private:
    scoped_ptr<EventManager> event_manager_;

    std::string server_ip_;
    uint16 server_port_;
    std::string client_ip_;
    uint16 client_port_;
    uint32 timeout_;

    Mutex ip_mutex_;
    scoped_ptr<ClientProcessor> processor_;
    scoped_ptr<Connector> connector_;

    scoped_ptr<Closure> reconnect_cb_;

    DISALLOW_COPY_AND_ASSIGN(RpcClient);
};
}

#endif
